package com.base.system.managedBean.baseBean;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.SelectItems;

import com.base.common.provider.SelectItemsProvider;
import com.base.organization.entity.DepartmentalUnit;

public abstract class BaseSecurityDepartmentBean implements Serializable {
	@Inject
	FacesContext context;
	@Accessible
	@Bind
	protected String departmentalUnitId;
	@Bind(id = "name", attribute = "value")
	protected String name;
	@Accessible
	private SelectItem[] enable_items = SelectItemsProvider.getYesNo();
	@Bind(id = "enable", attribute = "value")
	@SelectItems(source = "#{this.enable_items}")
	protected boolean enable = true;
	@Bind(id = "departmentCode", attribute = "value")
	protected String departmentCode;
	@Bind(id = "indexNumber", attribute = "value")
	protected Integer indexNumber;
	@Bind
	protected String centralUnitId;
	@Bind
	protected String parentDepartmentalUnitId;
	protected DepartmentalUnit departmentalUnit;

	protected abstract void initSpecialProperty();

	protected void setSecurityDepartmentProperty() {
		if (departmentalUnit == null)
			return;
		if (departmentalUnitId != null && !"".equals(departmentalUnitId))
			departmentalUnit.setDepartmentalUnitId(Integer
					.parseInt(departmentalUnitId));
		if (centralUnitId != null && !"".equals(centralUnitId))
			departmentalUnit.setCentralUnitId(Integer.parseInt(centralUnitId));
		if (parentDepartmentalUnitId != null
				&& !"".equals(parentDepartmentalUnitId))
			departmentalUnit.setParentDepartmentalUnitId(Integer
					.parseInt(parentDepartmentalUnitId));
		departmentalUnit.setEnable(enable);
		departmentalUnit.setName(name);
		departmentalUnit.setDepartmentCode(departmentCode);
		departmentalUnit.setIndexNumber(indexNumber);
	}

	protected void getDepartmentProperty() {
		if (departmentalUnit == null)
			return;
		departmentalUnitId = departmentalUnit.getDepartmentalUnitId()
				.toString();
		if (departmentalUnit.getCentralUnitId() != null)
			centralUnitId = departmentalUnit.getCentralUnitId().toString();
		if (departmentalUnit.getParentDepartmentalUnitId() != null)
			parentDepartmentalUnitId = departmentalUnit
					.getParentDepartmentalUnitId().toString();
		enable = departmentalUnit.isEnable();
		name = departmentalUnit.getName();
		departmentCode = departmentalUnit.getDepartmentCode();
		indexNumber = departmentalUnit.getIndexNumber();
	}
}
